﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Eshouse.Bll;
using Eshouse.Entity;
using System.Data;



namespace Eshouse.Web
{
   /// <summary>
   /// 后台登陆认证
   /// </summary>
    public class AdminLoginAuthAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            DataTable dt;
            if (httpContext.Cache["role"] != null)
            {
                dt = (DataTable)httpContext.Cache["role"];
            }
            else
            {
                dt = RoleAccess.GetRoleTable();
                httpContext.Cache.Add("role", dt, null, DateTime.Now.AddMinutes(10), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);
            }
            bool verify = false;
            string username = httpContext.User.Identity.Name;
            if (username == null)
                return verify;
            string role = personAccess.GetRoleNameByUserName(httpContext.User.Identity.Name);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["rolename"].ToString() != "一般用户")
                {
                    if (dt.Rows[i]["rolename"].ToString() == role)
                    {
                        verify = true;
                        break;
                    }
                }
            }
            return verify;
        }        
    }
    /// <summary>
    /// 一般用户登录认证
    /// </summary>
    public class userLoginAuthAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
           
            DataTable dt;
            if (httpContext.Cache["role"] != null)
            {
                dt = (DataTable)httpContext.Cache["role"];
            }
            else
            {
                dt = RoleAccess.GetRoleTable();
                httpContext.Cache.Add("role", dt, null, DateTime.Now.AddMinutes(10), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);
            }
            bool verify = false;
            string username = httpContext.User.Identity.Name;
            if (username == null)
                return verify;
            string role = personAccess.GetRoleNameByUserName(httpContext.User.Identity.Name);
            for (int i = 0; i < dt.Rows.Count; i++)
            {               
                    if (dt.Rows[i]["rolename"].ToString() == role)
                    {
                        verify = true;
                        break;
                    }                
            }
            return verify;
        }
    }
    /// <summary>
    /// 超级系统管理员
    /// </summary>
    public class SysAdminAuthAttribute : AuthorizeAttribute {

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
           
            DataTable dt;
            if (httpContext.Cache["role"] != null)
            {
                dt = (DataTable)httpContext.Cache["role"];
            }
            else
            {
                dt = RoleAccess.GetRoleTable();
                httpContext.Cache.Add("role", dt, null, DateTime.Now.AddMinutes(10), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);
            }
            bool verify = false;
            string username = httpContext.User.Identity.Name;
            if (username == null)
                return verify;
            string role = personAccess.GetRoleNameByUserName(httpContext.User.Identity.Name);
            if(role=="超级管理员")
                verify = true; ;
            return verify;
        
        }    
    
    }
    /// <summary>
    /// 一般管理员
    /// </summary>
    public class CommAdminAuthAttribute:AuthorizeAttribute {

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
           
            DataTable dt;
            if (httpContext.Cache["role"] != null)
            {
                dt = (DataTable)httpContext.Cache["role"];
            }
            else
            {
                dt = RoleAccess.GetRoleTable();
                httpContext.Cache.Add("role", dt, null, DateTime.Now.AddMinutes(10), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);
            }
            bool verify = false;
            string username = httpContext.User.Identity.Name;
            if (username == null)
                return verify;
            string role = personAccess.GetRoleNameByUserName(httpContext.User.Identity.Name);
            if(role=="普通管理员")
               verify=true ;
            return verify;    
        } 
    
    }


}
